﻿using System;
using System.Runtime.Serialization;

namespace NetBay.Core.BusinessEntities.Versioning
{
    [DataContract]
    public class VersionNumber : ReturnObject, ICloneable, IComparable, IComparable<VersionNumber>, IEquatable<Version>
    {
        #region Attributs
        private int _major;
        private int _minor;
        #endregion

        #region Properties

        /// <summary>
        /// Get or Set the Major version number, IndexOutOfRangeException raised if number is less than zéro.
        /// </summary>
        [DataMember]
        public int Major 
        { 
            get
            {
                return _major;
            }
            set
            {
                if (_major < 0)
                    throw new IndexOutOfRangeException("Major number cannot be less than zero.");
                else
                    _major = value;
            }
        }

        /// <summary>
        /// Get or Set the Minor version number, IndexOutOfRangeException raised if number is less than zéro.
        /// </summary>
        [DataMember]
        public int Minor
        {
            get
            {
                return _minor;
            }
            set
            {
                if (_minor < 0)
                    throw new IndexOutOfRangeException("Minor number cannot be less than zero.");
                else
                    _minor = value;
            }
        }

        #endregion

        #region Constructors

        public VersionNumber()
            : this(0, 0)
        {
        }

        public VersionNumber(int Major)
            :this(Major, 0)
        {
        }

        public VersionNumber(int Major, int Minor)
        {
            this.Major = Major;
            this.Minor = Minor;
        }

        #endregion

        #region Functions

        public new object Clone()
        {
            VersionNumber _version = new VersionNumber();
            _version.Major = this.Major;
            _version.Minor = this.Minor;
            return _version;
        }

        public override string ToString()
        {
            return string.Format("Version {0}.{1}", Major, Minor);
        }

        public string ToString(int patern)
        {
            string temp = string.Empty;
            switch (patern)
            {
                case 0:
                    temp = string.Empty;
                    break;

                case 1:
                    temp = string.Format("Version {0}", Major);
                    break;

                case 2:
                    temp = string.Format("Version {0}.{1}", Major, Minor);
                    break;
            }
            return temp;
        }

        public int CompareTo(object version)
        {
            if (version == null)
            {
                return 1;
            }
            VersionNumber version2 = version as VersionNumber;
            if (version2 == null)
            {
                throw new ArgumentException("version must be a type VersionNumber");
            }
            if (this.Major != version2.Major)
            {
                if (this.Major > version2.Major)
                {
                    return 1;
                }
                return -1;
            }
            if (this.Minor != version2.Minor)
            {
                if (this.Minor > version2.Minor)
                {
                    return 1;
                }
                return -1;
            }
            return -1;
        }

        public int CompareTo(VersionNumber value)
        {
            if (value == null)
            {
                return 1;
            }
            if (this.Major != value.Major)
            {
                if (this.Major > value.Major)
                {
                    return 1;
                }
                return -1;
            }
            if (this.Minor != value.Minor)
            {
                if (this.Minor > value.Minor)
                {
                    return 1;
                }
                return -1;
            }
            return -1;
        }

        public bool Equals(Version other)
        {
            if (other == null)
            {
                return false;
            }
            return (((this.Major == other.Major) && (this.Minor == other.Minor)));

        }

        #endregion

    }
}
